package com.xianzhiOA.service.impl;/**
 * Created by Administrator on 2017/5/2.
 */

import com.github.pagehelper.PageHelper;
import com.xianzhiOA.bean.XzAttendance;
import com.xianzhiOA.bean.XzEmp;
import com.xianzhiOA.bean.XzWork;
import com.xianzhiOA.dao.XzAttendanceMapper;
import com.xianzhiOA.dao.XzEmpMapper;
import com.xianzhiOA.dao.XzWorkMapper;
import com.xianzhiOA.service.XzWorkService;
import com.xianzhiOA.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * XzWorkServiceImpl
 *
 * @author 甘汝雷
 * @create 2017-05-02 11:04
 **/
@Service("xzWorkService")
public class XzWorkServiceImpl implements XzWorkService{
    @Autowired
    private XzWorkMapper workMapper;
    @Autowired
    private XzAttendanceMapper attendanceMapper;
    //添加工作量
    @Override
    public int insert(XzWork work) {
        Date date = work.getWorkWeekTime();
        work.setWorkWeek(getWeekOfDate(date));//添加星期
        return workMapper.insert(work);
    }

    @Override
    //查询员工实际工作时间
    public double workActual(XzWork work,Date date){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("empNo",work.getEmpId());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String s = sdf.format(date);
        Date dateA = null;
        try {
            dateA =  sdf.parse(s);
        }catch (Exception e){}
        map.put("beginDate",dateA);
        map.put("endDate",myDate(dateA));
        //获取员工指定日期的打卡信息
        List<XzAttendance> list = attendanceMapper.selAll(map);
        if (list.isEmpty())return 0;
        Date timeFirst = dateA;
        Date timeLast = myTime(dateA);
        //查找员工的实际工作时间
        for (XzAttendance a :list){
            Date timeA = a.getFirstTime();//上班时间
            Date timeB = a.getLastTime();//下班时间
            if (timeA!=null&&timeB!=null) {
                if (timeA.getTime() - timeFirst.getTime() > 0) {
                    timeFirst = timeA;
                }
                if (timeLast.getTime()-timeB.getTime() > 0) {
                    timeLast = timeB;
                }
            }else return 0;
        }
        return getDatePoor(timeFirst,timeLast);
    }
    //判断begin与end的时间差
    public static double getDatePoor(Date begin, Date end) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = end.getTime() - begin.getTime();
        double hour = (double)(diff % nd / nh);// 计算差多少小时
        double min = (double)(diff % nd % nh/nm);//计算差多少分钟
        BigDecimal b = new BigDecimal(hour+min/60);
        double f = b.setScale(1,BigDecimal.ROUND_HALF_UP).doubleValue();
        return f;
    }

    @Override
    //判断date星期几
    public String getWeekOfDate(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK)-1;
        if (w < 0) w = 0;
        return weekDays[w];
    }
    //真删除
    @Override
    public int delete(Long workId) {
        return workMapper.delete(workId);
    }
    //伪删除
    @Override
    public int isdelete(Long workId){
        Map map = new HashMap();
        map.put("workId",workId);
        map.put("isdelete",1);
        return workMapper.update(map);
    }
    //修改工作量
    @Override
    public int update(XzWork work,String empRole) {
        work.setWorkWeekTime(myTime(work.getWorkWeekTime()));//保存日期
        work.setWorkWeek(getWeekOfDate(work.getWorkWeekTime()));//添加星期
        int i = 0;
        if(empRole.equals("1")){
            Map map =myMap(work);
            map.put("workId",work.getWorkId());
            map.put("workWeekTime",work.getWorkWeekTime());
            workMapper.update(map);
            i++;
        }else {
            Map map = new HashMap();
            map.put("workId",work.getWorkId());
            map.put("workWeekTime",work.getWorkWeekTime());
            map.put("workWeek",work.getWorkWeek());
            map.put("workPlan",work.getWorkPlan());
            workMapper.update(map);
        }
        return i;
    }

    //double类型数据保留小数点后一位
    public double myDouble(double d1,double d2){
        BigDecimal bd = new BigDecimal(d1-d2);
        return bd.setScale(1,BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    //按周查询
    @Override
    public List<XzWork> selAll(XzWork work) {
        Map map = myMap(work);
        map.putAll(getWeekByDate(work.getWorkWeekTime()));
        List<XzWork> works = workMapper.selAll(map);
        for (XzWork x: works) {
            x.setDeviation(myDouble(x.getWorkActual(), x.getWorkPlan()));//保存偏差值
            x.setRate(myDouble(x.getDeviation(), x.getWorkPlan()));//保存偏差率
        }
        return works;
    }

    //查询当天记录是否存在
    @Override
    public boolean selDate(XzWork work){
        Map map = new HashMap();
        map.put("empId",work.getEmpId());
        map.put("begin",work.getWorkWeekTime());
        map.put("end",myDate(work.getWorkWeekTime()));
        List<XzWork> works = workMapper.selAll(map);
        if (!works.isEmpty()){
            for (XzWork w:works){
                if (w.getWorkId()!=work.getWorkId())return true;
            }
        }
        return false;
    }
    //把work转换成map类型
    public Map myMap(XzWork work){
        Map<String, Object> map = new HashMap<>();
        map.put("empId",work.getEmpId());
        map.put("weekId",work.getWeekId());
        map.put("workWeek",work.getWorkWeek());
        map.put("workPlan",work.getWorkPlan());
        map.put("workActual",work.getWorkActual());
        map.put("field1",work.getField1());
        map.put("field2",work.getField2());
        map.put("field3",work.getField3());
        map.put("isdelete",work.getIsdelete());
        return map;
    }

    //通过id查询
    @Override
    public XzWork selById(Long workId) {
        return workMapper.selById(workId);
    }

    //给date加时1天
    public Date myDate(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 24);
        return c.getTime();
    }
    //给date加时23:59:59
    @Override
    public Date myTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        return c.getTime();
    }
    //给date增加1小时
    public Date myDateS(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 1);
        return c.getTime();
    }

    //根据date返回该周的开始时间和结束时间
    @Override
    public Map getWeekByDate(Date time) {
        Map map = new HashMap();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        int day = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        String impTimeBegin = sdf.format(cal.getTime());//一周的开始时间
        map.put("begin",impTimeBegin);
        cal.add(Calendar.DATE, 7);
        String impTimeMi = sdf.format(cal.getTime());//一周的结束时间
        map.put("end",impTimeMi);
        return map;
    }
}
